package cn.sharp.bbs.service.impl;

import cn.sharp.bbs.dao.user.UserDao;
import cn.sharp.bbs.dao.gen.UserMapper;
import cn.sharp.bbs.entity.BO.UserStats;
import cn.sharp.bbs.entity.DO.User;
import cn.sharp.bbs.event.FollowEvent;
import cn.sharp.bbs.service.UserService;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class UserServiceImpl implements UserService, ApplicationEventPublisherAware {
    @Resource
    UserMapper userMapper;
    @Resource
    UserDao userDao;

    private ApplicationEventPublisher eventPublisher;
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.eventPublisher = applicationEventPublisher;
    }
    /**
     * 检查账号是否无效
     * @param account 用户账号
     * @return 布尔值, 代表账号是否无效
     */
    @Override
    public boolean isInvalidAccount(String account) {
        return userDao.selectUserByAccount(account)==null;
    }

    /**
     * 获取用户简要信息
     * @param account 用户账号
     * @return 用户简要信息
     */
    @Override
    public User getUserBrief(String account) {
        User user = userDao.selectUserByAccount(account);
        user.setPwd("");
        user.setMail("");
        user.setStime(null);
        return user;
    }

    /**
     * 获取用户统计信息
     * @param account 用户账号
     * @return 用户统计信息
     */
    @Override
    public UserStats getUserStats(String account) {
        Long uid = userDao.selectUserByAccount(account).getId();
        UserStats userStats = new UserStats();
        userStats.setUid(uid);
        userStats.setCommentNum(userDao.getCommentNum(uid));
        userStats.setCollectNum(userDao.getCollectNum(uid));
        userStats.setFansNum(userDao.getFansNum(uid));
        userStats.setFollowNum(userDao.getFollowNum(uid));
        userStats.setLikeReceived(userDao.getLikeReceived(uid));
        userStats.setPostNum(userDao.getPostNum(uid));
        return userStats;
    }


    /**
     * 获取用户详情
     * @param account 用户账号
     * @return 用户详情, 未传递密码
     */
    @Override
    public User getUserInfo(String account) {
        User user = userDao.selectUserByAccount(account);
        user.setPwd("");
        return user;
    }

    /**
     * 修改用户信息, 不允许修改密码
     * @param user 用户信息
     * @return 修改结果
     */
    @Override
    public String doEditUser(User user) {
        User before = userMapper.selectByPrimaryKey(user.getId());
        user.setReg(before.getReg());
        user.setPwd(before.getPwd()); // 密码修改另有其位置
        if(!before.getNum().equals(user.getNum())&&userDao.selectUserByAccount(user.getNum())!=null)
            return "该账号已存在";
        if(!before.getMail().equals(user.getMail())&&userDao.selectUserByMail(user.getMail())!=null)
            return "该邮箱已被使用";
        userMapper.updateByPrimaryKey(user);
        return null;
    }

    /**
     * 关注用户
     * @param u_id 关注发起人id
     * @param account 关注目标账号
     */
    @Override
    public void followUser(Long u_id, String account) {
        User target = userDao.selectUserByAccount(account);
        userDao.followUser(u_id, target.getId());
        this.eventPublisher.publishEvent(new FollowEvent(this, userDao.getFollowId(u_id, target.getId())));
    }

    /**
     * 取关用户
     * @param u_id 动作发起人id
     * @param account 目标用户账号
     */
    @Override
    public void unfollowUser(Long u_id, String account) {
        User target = userDao.selectUserByAccount(account);
        userDao.unfollowUser(u_id, target.getId());
    }

    /**
     * 检查关注关系
     * @param u_id 关注发起人
     * @param account 被关注用户
     * @return 布尔值, 关注关系是否存在
     */
    @Override
    public boolean isFollowed(Long u_id, String account) {
        User target = userDao.selectUserByAccount(account);
        return userDao.countFollowRelation(u_id, target.getId())>0;
    }
}
